package com.ccp.dev.extend.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.consts.SystemConst;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.extend.dao.ExtendSysUserDao;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysUser;
import com.ccp.dev.system.service.SysOrgService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 扩展的用戶管理service
 *
 * @author 创新中心-王明亮
 */
@Service
public class ExtendSysUserService extends BaseService<SysUser> {
    @Resource
    SysOrgService sysOrgService;
    @Resource
    ExtendSysOrgService extendSysOrgService;
    @Resource
    ExtendSysUserDao extendSysUserDao;

    @Value("${mybatis.configuration.database-id}")
    String dataSourceType;

    @Value("${server.imModel.load}")
    private Boolean loadIm = false;


    /**
     * 根据租户查询列表
     *
     * @param queryFilter 条件filter
     * @return list
     */
    public List<SysUser> listByTenTree(QueryFilter queryFilter) {
        SysUser sysUser = (SysUser) ContextUtil.getCurrentUser();
        if (!queryFilter.containsKey("dataPrivacyLevel")) {
            int privacyLevel = sysUser.getPrivacyLevel();
            queryFilter.put("privacyLevel", privacyLevel);
        }
        List<String> tenantIds = new ArrayList<>();
        List<SysOrg> sysOrgList = new ArrayList<>();
        if (StringUtil.isEmpty(queryFilter.get("orgId")) && !SystemConst.SUPER_ADMIN_ID.equals(sysUser.getUserId())) {
            // 登录人所在租户
            String tenantId = ContextUtil.getCurrentUserTenantId(ContextUtil.getCurrentUserId());
            // 根据租户id查找组织
            SysOrg sysOrg = sysOrgService.getOrgByTenId(tenantId);
            // 根据组织id查找其所有子组织
            sysOrgList = extendSysOrgService.querySubSysOrgBySupOrgId(sysOrg.getOrgId());
        } else if (StringUtil.isNotEmpty(String.valueOf(queryFilter.get("orgId")))) {
            // 根据组织id查找其所有子组织
            sysOrgList = extendSysOrgService.querySubSysOrgBySupOrgId(String.valueOf(queryFilter.get("orgId")));
        }
        // 如果获取到的组织中没有当前组织，手动加进去
        boolean noneMatch = sysOrgList.stream().noneMatch(sysOrg -> sysOrg.getOrgId().equals(ContextUtil.getCurrentOrgId()));
        if (noneMatch) {
            sysOrgList.add((SysOrg) ContextUtil.getCurrentOrg());
        }
        // 取出所有组织的租户id放到集合中
        for (SysOrg sysOrg : sysOrgList) {
            String tenantId = sysOrgService.getTenantIdByOrgId(sysOrg.getOrgId());
            if (!tenantIds.contains(tenantId)) {
                tenantIds.add(tenantId);
            }
        }
        // 加入租户id查询条件，去除组织id查询条件
        queryFilter.addFilter("tenantIds", tenantIds);
        queryFilter.remove("orgId");
        // 查询到的数据
        List<SysUser> sysUserList = extendSysUserDao.queryUsersByTenOrg(queryFilter);
        // 把所属单位是部门的循环遍历替换成租户组织
        for (SysUser user : sysUserList) {
            String orgId = user.getOrgId();
            int isTenant = sysOrgService.getOneById(orgId).getIsTenant();
            if (isTenant != 1) {
                String tenantId = sysOrgService.getTenIdByOrgId(orgId);
                SysOrg sysOrg = sysOrgService.getOrgByTenId(tenantId);
                user.setOrgId(sysOrg.getOrgId());
                user.setOrgName(sysOrg.getOrgName());
            }
        }
        return sysUserList;
    }

    /**
     * 根据用户姓名获取用户列表(精确匹配)
     *
     * @param fullname 用户姓名
     * @return SysUser
     */
    public List<SysUser> getByFullname(String fullname) {
        return extendSysUserDao.getByFullname(fullname);
    }



    public List<SysUser> queryUsersByOrg(String path){
            Map<String,Object> map=new HashMap<>();
            map.put("path",path);
            return extendSysUserDao.queryUsersByOrg(map);
    }
}
